package com.lnkj.spcj.util;

import org.greenrobot.eventbus.EventBus;

/**
 * @Description EventBus工具类
 * @Date 2018/10/11 0011 上午 9:49
 * @CreateBy Android Studio
 */
public class EventBusUtils {
    public EventBusUtils() {
    }

    /**
     * 注册 EventBus
     *
     * @param subscriber
     */
    public static void register(Object subscriber) {
        EventBus eventBus = EventBus.getDefault();
        if (!eventBus.isRegistered(subscriber)) {
            eventBus.register(subscriber);
        }
    }

    /**
     * 解除注册 EventBus
     *
     * @param subscriber
     */
    public static void unregister(Object subscriber) {
        EventBus eventBus = EventBus.getDefault();
        if (eventBus.isRegistered(subscriber)) {
            eventBus.unregister(subscriber);
        }
    }

    /**
     * 发送事件消息
     *
     * @param event
     */
    public static void post(EventMessage event) {
        EventBus.getDefault().post(event);
    }

    /**
     * 发送粘性事件消息
     *
     * @param event
     */
    public static void postSticky(EventMessage event) {
        EventBus.getDefault().postSticky(event);
    }

    public static class EventMessage<T> {

        private int code;
        private String id;
        private String headImgs;
        private T data;

        public EventMessage(int code) {
            this.code = code;
        }

        public EventMessage(int code, T data) {
            this.code = code;
            this.data = data;
        }

        public EventMessage(int code, String id, T data) {
            this.code = code;
            this.id = id;
            this.data = data;
        }

        public EventMessage(int code, String id, String imgs, T data) {
            this.code = code;
            this.id = id;
            this.headImgs = imgs;
            this.data = data;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getHeadImgs() {
            return headImgs;
        }

        public void setHeadImgs(String headImgs) {
            this.headImgs = headImgs;
        }

        @Override
        public String toString() {
            return "EventMessage{" +
                    "code=" + code +
                    ", data=" + data +
                    '}';
        }
    }

    public class EventCode {
        public static final int EVENT_RELOGIN = 100;
        public static final int EVENT_WXLOGIN = 101;
    }
}
